﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Travelers.Code.TaobaoTour
{
    static class x
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Traveler());
        }
    }
    public class Traveler : Form
    {
        private WebBrowser wb;
        private TabControl tabControl1;
        private TabPage tabPage1;
        private TabPage tabPage2;
        private Button button1;
        private Button button2;
        private Queue<string> q;
        private List<string> list;
        private bool isWork;
        private bool isNav;

        public Traveler()
        {
            this.tabControl1 = new TabControl();
            this.tabPage1 = new TabPage();
            this.tabPage2 = new TabPage();
            this.wb = new WebBrowser();
            this.button1 = new Button();
            this.button2 = new Button();
            q = new Queue<string>();
            list = new List<string>();
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabControl1.Location = new System.Drawing.Point(0, 0);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(284, 264);
            this.tabControl1.TabIndex = 0;
            // 
            // tabPage1
            // 
            this.tabPage1.Location = new System.Drawing.Point(4, 22);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage1.Size = new System.Drawing.Size(276, 238);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "浏览";
            this.tabPage1.UseVisualStyleBackColor = true;
            // 
            // tabPage2
            // 
            this.tabPage2.Controls.Add(this.button1);
            this.tabPage2.Controls.Add(this.button2);
            this.tabPage2.Location = new System.Drawing.Point(4, 22);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage2.Size = new System.Drawing.Size(276, 238);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "管理";
            this.tabPage2.UseVisualStyleBackColor = true;

            this.button1.Location = new System.Drawing.Point(18, 20);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "启动";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);

            this.button2.Location = new System.Drawing.Point(118, 20);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 0;
            this.button2.Text = "暂停";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);

            wb.Dock = DockStyle.Fill;
            wb.AllowWebBrowserDrop = false;
            wb.WebBrowserShortcutsEnabled = false;
            wb.ScrollBarsEnabled = false;
            wb.IsWebBrowserContextMenuEnabled = false;
            this.tabPage1.Controls.Add(wb);

            this.Text = "Traveler";
            this.ClientSize = new System.Drawing.Size(600, 480);
            this.Controls.Add(this.tabControl1);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            wb.Navigate("http://member1.taobao.com/member/login.jhtml?ssl=false&f=top&redirect_url=http%3A%2F%2Fwww.taobao.com%2Fvertical%2Fspace%2F");
            wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
        }

        void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (isWork)
            {
                isNav = false;
                string source = wb.Document.Body.InnerHtml;

                //[code]

                //--------------------------
                
                source = source.Replace("<LI id=", "<xxxid=");
                Match m = Regex.Match(source, "<xxxid=(.*?)>", RegexOptions.Singleline);
                while (m.Success)
                {
                    Group g1 = m.Groups[1];
                    string url = g1.ToString();
                    int l = url.Length;
                    if (l == 32)
                    {
                        url = url.ToLower();
                        string preUrl = "http://space.taobao.com/";
                        url = preUrl + url + ".htm";
                        if (!list.Contains(url))
                        {
                            list.Add(url);
                            q.Enqueue(url);
                        }
                    }
                    m = m.NextMatch();
                }
               
                //---------------------------------
                if (!isNav)
                {
                    if (q.Count > 0)
                    {
                        wb.Navigate(q.Dequeue());
                        isNav = true;
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            isWork = true;
            wb.Navigate("http://space.taobao.com/802ce1b82a91a39ff5ecdcb5ad32d101.htm");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            isWork = false;
        }
    }
}

